package com.algorithm;


import java.util.Arrays;

/**
 * 加一
 */
public class PlusOne {

    public static int[] plusOne(int[] digits) {
        int nums[] = new int[digits.length + 1];
        int n = digits.length;
        int temp = 0;
        for (int i = digits.length - 1; i >= 0; i--) {
            int result;
            if (i == digits.length - 1) {
                result = digits[i] + 1 + temp;
            } else {
                result = digits[i] + temp;
            }
            nums[n] = result % 10;
            temp = result / 10;
            n--;
        }
        if (temp > 0) {
            nums[0] = 1;
        } else {
            return Arrays.copyOfRange(nums, 1, digits.length+1);
        }
        return nums;
    }

    public static void main(String[] args) {
//        int[] nums = new int[]{1, 2, 3, 4};
        int[] nums = new int[]{1, 2, 3, 9};
        int[] ints = plusOne(nums);
        System.out.println(ints);
    }
}
